package org.spring.bse.home.util;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public final class HTMLUtil
{

	public static Elements getElementsByTagNameAndAttr(Element inputElement, String tagName, String... str)						
	{
		Elements filteredElements = null;
		Elements outputElements = new Elements();

		if (inputElement == null || str.length % 2 != 0 || tagName == null || tagName.isEmpty())
		{
			return null;
		}

		filteredElements = inputElement.getElementsByTag(tagName);

		for (Element ele : filteredElements)
		{
			boolean isAFind = true;
			for (int i = 0; i < str.length; i += 2)
			{
				String attrVal = ele.attr(str[i]);
				if (attrVal == null || !attrVal.equals(str[i + 1]))
				{
					isAFind = false;
				}
			}
			if (isAFind)
			{
				outputElements.add(ele);
			}
		}

		return outputElements;
	}

	public static Element getElementByTagNameAndAttr(Element inputElement, String tagName, String... str)
	{
		Element ele = null;
		Elements outputElements = getElementsByTagNameAndAttr(inputElement, tagName, str);
		if (outputElements != null)
		{
			ele = outputElements.get(0);
		}
		return ele;
	}
}
